<template>
  <div>
    <van-list v-model:loading="loading" :finished="finished" @load="onLoad">
      <div v-for="(item, index) in lists" :key="index" class="couponcard">
        <div class="couponcard1">
          <i>￥</i>{{ item.voucherAmount }}
          <p>无门槛券</p>
        </div>
        <div class="couponcard2">
          <p class="couponcard2-1">{{ item.voucherName }}</p>
          <p class="couponcard2-2">有效期</p>
          <p class="couponcard2-2">{{ item.startTime }}至{{ item.endTime }}</p>
        </div>
        <div class="couponcard3" @click="getReceive(item)">领取</div>
      </div>
      <van-empty :image="empty" image-size="200"  description="暂无数据" v-if="!lists.length && loading == false" />
    </van-list>
    <van-dialog v-model:show="show" :showConfirmButton="false" :showCancelButton="false" class="vanDialog">
      <img class="dialogImg" src="@/assets/activity/icon_cg.png" alt="">
      <p class="p1">恭喜您领取成功</p>
      <p class="p2">可在我的-优惠券中查看</p>
      <div class="gosee" @click="getCouponDetail">去看看</div>
      <div class="iknow" @click="iknow">我知道了</div>
    </van-dialog>
  </div>
</template>
<script setup lang="ts">
import { ref } from "vue";
import { useRouter } from 'vue-router'
import { getVoucherUnReceiveList, getReceiveVoucher } from "@/services/home";
import empty from "@/assets/empty.png";
const router = useRouter()
const show = ref(false);
const total = ref();
const loading = ref(false);
const finished = ref(false);
const pageNum = ref(1);
const lists = ref([]);
// 上拉加载
const onLoad = () => {
  console.log('优惠券列表加载');
  getVoucherUnReceiveList({
    pageNum: pageNum.value++
  }).then(res => {
    if (res.code == 200) {
      lists.value = lists.value.concat(res.rows)
      total.value = res.total
      // 加载状态结束
      loading.value = false;
      // 数据全部加载完成
      if (lists.value.length >= total.value) {
        finished.value = true;
      }
    }
  })
};
// 领取优惠券
const getReceive = (e: any) => {
  show.value = true
  getReceiveVoucher({ id: e.id }).then(res => {
    if (res.code === 200) {
      // getReceiveList()
    }
  })
};
// 点前往已经领取详情
const getCouponDetail = () => {
  show.value = false
  router.push('/user/coupon')
};
// 点我知道
const iknow = () => {
  show.value = false
  getReceiveList()
}

// 获取列表
const getReceiveList = () => {
  getVoucherUnReceiveList().then(res => {
    lists.value = res.rows
  })
}
</script>
<style lang="scss" scoped>
.couponcard {
  display: flex;
  // justify-content: space-around;
  align-items: center;
  width: 343px;
  height: 92px;
  background: url(@/assets/activity/bg.png) no-repeat;
  background-size: cover;
  margin-bottom: 10px;

  // padding: 0 12px;
  .couponcard1 {
    margin-left: 15px;
    text-align: center;
    width: 60px;
    font-size: 20px;
    color: #f65d3e;

    i {
      font-style: inherit;
      font-size: 12px;
    }

    p {
      font-size: 12px;
    }
  }

  .couponcard2 {
    margin-left: 20px;
    text-align: center;
    width: 160px;

    .couponcard2-1 {
      width: 160px;
      font-size: 16px;
      text-overflow: ellipsis;
      overflow: hidden;
      white-space: nowrap;
    }

    .couponcard2-2 {
      color: rgba(163, 174, 182, 1);
      font-size: 12px;
    }
  }

  .couponcard3 {
    color: #fff;
    font-size: 20px;
    margin-left: 30px;
  }
}

.vanDialog {
  .gosee {
    margin: auto;
    width: 300px;
    height: 40px;
    background-color: #B80C17;
    border-radius: 100px;
    text-align: center;
    line-height: 40px;
    color: #fff;
    margin-bottom: 10px;
  }

  .iknow {
    margin: auto;
    width: 300px;
    height: 40px;
    background-color: #fff;
    border-radius: 100px;
    text-align: center;
    line-height: 40px;
    color: #c5c5c5;
    border: 1px solid #c5c5c5;
    margin-bottom: 10px;
  }

  .dialogImg {
    display: block;
    width: 50px;
    height: 50px;
    margin: 20px auto;
  }

  .p1 {
    text-align: center;
    margin-bottom: 5px;
    font-size: 18px;
  }

  .p2 {
    text-align: center;
    margin-bottom: 30px;
    font-size: 12px;
  }
}

::v-deep.van-loading__text {
  margin-left: 0;
}
</style>